from PySide6.QtWidgets import QWidget,QApplication
from PySide6.QtCore import Qt,QEvent
from PySide6.QtGui import QKeySequence,QShortcut
from ui.ui_form import Ui_Widget


class Widget(QWidget):
    def __init__(self, application, parent=None):
        super().__init__(parent)
        self.application = application
        self.ui = Ui_Widget()
        self.ui.setupUi(self)
        self.ui.TranslateButton.clicked.connect(self.onClickTranslateButton);
        self.ui.LockButton.clicked.connect(self.onClickLockButton)
        self.ui.OpenButton.clicked.connect(self.onClickOpenButton)
        self.ui.Translate.setReadOnly(True)
        self.ui.Original.returnPressed.connect(self.onClickTranslateButton)
        self.ui.Original.installEventFilter(self)
    def keyPressEvent(self, event):
        if event.modifiers() == Qt.KeyboardModifier.ControlModifier:
            if event.key() == Qt.Key.Key_C:
                QApplication.clipboard().setText(self.ui.Translate.text())
                self.ui.Original.clear()
            elif event.key() == Qt.Key.Key_Q:
                temp = self.ui.LockButton.isChecked()
                self.ui.LockButton.setChecked(not temp)
                self.onClickLockButton(not temp)
    def eventFilter(self, watched, event):
        if watched == self.ui.Original and event.type() == QEvent.Type.KeyPress:
            if event.modifiers() & Qt.KeyboardModifier.ControlModifier:
                if event.key() == Qt.Key.Key_C:
                    QApplication.clipboard().setText(self.ui.Translate.text())
                    self.ui.Original.clear()
                    return  True
                elif event.key() == Qt.Key.Key_Q:
                    temp = self.ui.LockButton.isChecked()
                    self.ui.LockButton.setChecked(not temp)
                    self.onClickLockButton(not temp)
        return super().eventFilter(watched, event)


    #点击事件
    def onClickTranslateButton(self):
        if self.ui.Original.text() == '':
            self.ui.Original.setStyleSheet('border: 1px solid red;')
            return
        else:
            self.ui.Translate.setText(self.application.save(self.ui.Original.text().lower()))

    # 点击事件
    def onClickLockButton(self, checked: bool):
        self.setWindowFlag(Qt.WindowStaysOnTopHint, checked)
        self.show()

    # 点击事件
    def onClickOpenButton(self):
        self.application.openEnglishFile();
